package hellfire.system.graphics;

import java.util.List;

public class GLSpriteAnim implements GLDrawable 
{
	private int[] sequence;
	private GLSpriteAtlas atlas;
	private int sequenceId = 0;
	private boolean isLooping;
	private boolean isPlaying = false;
	
	public GLSpriteAnim(GLSpriteAtlas atlas, int[] sequence, boolean isLooping)
	{
		this.sequence = new int[sequence.length];
		for(int i = 0; i < sequence.length; i++)
			this.sequence[i] = sequence[i];
		this.atlas = atlas;
		this.isLooping = isLooping;
	}
	
	public GLSpriteAnim(GLSpriteAtlas atlas, List<Integer> sequence, boolean isLooping)
	{
		for(int i = 0; i < sequence.size(); i++)
			this.sequence[i] = sequence.get(i);
		this.atlas = atlas;
		this.isLooping = isLooping;
	}
	
	/**
	 * Play from where we left off
	 */
	public void setPlaying()
	{
		isPlaying = true;
	}
	
	/**
	 * Stop and reset the animation.
	 * Use pause() to keep the state.
	 */
	public void setStopped()
	{
		setPaused();
		sequenceId = 0;
	}
	
	public void setPaused()
	{
		isPlaying = false;
	}
	
	public void setPlayFromStart()
	{
		setStopped();
		setPlaying();
	}
	
	@Override
	public void draw(float[] matrix) 
	{
		atlas.draw(matrix);
		sequenceId++;
		if(sequenceId == sequence.length)
		{
			if(isLooping)
				sequenceId = 0;
			else sequenceId--;
		}
		atlas.setSpriteId(sequence[sequenceId]);
	}
	
	public GLVec2 getPos() 
	{
		return atlas.getPos();
	}
	
	public void setPos(GLVec2 pos) 
	{
		atlas.setPos(pos);
	}
}
